package lockTest;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

/**
 * @author      : yang.xp
 * @date        : 2022/9/20 20:43
 * @description : 方法区溢出，元空间
 */
public class JavaMethodAreaOOM {

    // -Xmx20M -XX:MetaspaceSize=20m -XX:MaxMetaspaceSize=20m 由于cglib在方法区生成很多的类对象导致溢出
    // java.lang.OutOfMemoryError: Metaspace

    public static void main(String[] args) {
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(OOMObject.class);
            enhancer.setUseCache(false);
            enhancer.setCallback((MethodInterceptor) (obj, method, args1, proxy) -> proxy.invokeSuper(obj, args1));
            System.out.println(enhancer.create().getClass());
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class OOMObject {

}